package com.ysstech.valuation.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ysstech.common.cache.MapCacheManager;
import com.ysstech.common.cache.RedisCacheManager;
import com.ysstech.valuation.entity.BondFaceMoneyInfo;
import com.ysstech.valuation.mapper.BondFaceMoneyInfoMapper;
import com.ysstech.valuation.service.BondFaceMoneyInfoService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * <p>
 * 债券票面金额信息表 服务实现类
 * </p>
 *
 * @author lishuangliang
 * @since 2021-08-11
 */
@Service
public class BondFaceMoneyInfoServiceImpl extends ServiceImpl<BondFaceMoneyInfoMapper, BondFaceMoneyInfo> implements BondFaceMoneyInfoService {
    @Autowired
    private RedisCacheManager redisCacheManager;

    /**
     * 查询一个时间点所有债券的票面价格
     *
     * @param bondFaceMoneyInfo
     * @return
     * @throws Exception
     */
    @Override
    public Map<String, BondFaceMoneyInfo> queryBondFaceMoneyInfo(BondFaceMoneyInfo bondFaceMoneyInfo) throws Exception {
        //获取债券票面价格
        Map<String, BondFaceMoneyInfo> mapPrice = null;
        try {
            if (redisCacheManager.exists("BONDFACEPRICE\t" + bondFaceMoneyInfo.getStartAdjustRateDate())) {
                return (Map<String, BondFaceMoneyInfo>) MapCacheManager.getInstance().getCache("BONDFACEPRICE\t"
                        + bondFaceMoneyInfo.getStartAdjustRateDate());
            }
            QueryWrapper<BondFaceMoneyInfo> wrapper = new QueryWrapper<>();
            wrapper.ge("START_ADJUST_RATE_DATE", bondFaceMoneyInfo.getStartAdjustRateDate());
            wrapper.le("END_ADJUST_RATE_DATE", bondFaceMoneyInfo.getStartAdjustRateDate());
            List<BondFaceMoneyInfo> facePriceList = this.list(wrapper);
            if (null == facePriceList || facePriceList.size() == 0) {
                return mapPrice;
            }
            mapPrice = new HashMap<>();
            for (BondFaceMoneyInfo facePrice : facePriceList) {
                mapPrice.put(facePrice.getSecurityCode(), facePrice);
            }
            redisCacheManager.putCache("BONDFACEPRICE\t" + bondFaceMoneyInfo.getStartAdjustRateDate(), mapPrice);
        } catch (Exception ex) {
            log.error("获取债券票面价格错误!" + ex);
            throw new Exception("获取债券票面价格错误，请稍后重试!");
        }
        return mapPrice;
    }

    /**
     * 缓存中获取一个债券的当前时间的票面价格
     *
     * @param bondFaceMoneyInfo
     * @return
     * @throws Exception
     */
    public BondFaceMoneyInfo queryBondFaceMoneyInfoCache(BondFaceMoneyInfo bondFaceMoneyInfo) throws Exception {
        try {
            if (null == bondFaceMoneyInfo || null == bondFaceMoneyInfo.getStartAdjustRateDate()
                    || StringUtils.isBlank(bondFaceMoneyInfo.getSecurityCode())) {
                return null;
            }
            Map<String, BondFaceMoneyInfo> mapCorre = this.queryBondFaceMoneyInfo(bondFaceMoneyInfo);
            if (null == mapCorre || mapCorre.size() == 0) {
                return null;
            }
            return mapCorre.get(bondFaceMoneyInfo.getSecurityCode());
        } catch (Exception ex) {
            log.error("获取债券票面价格错误" + ex.toString());
            throw new Exception("获取债券票面价格错误，请稍后重试");
        }
    }


}
